package com.sacodemel.financeiro.persistencia.dao.impl;

import com.sacodemel.financeiro.persistencia.dao.DAO;
import com.sacodemel.financeiro.persistencia.dao.factory.HibernateDAOFactory;
import com.sacodemel.financeiro.util.Constantes;
import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class BaseDAO<T> extends HibernateDAOFactory implements DAO<T>, Serializable {

	
	private Class<T> clazz;

	/**
	 * Construtor padrão.
	 * 
	 * @param clazz
	 *            Classe a ser manipulada por este DAO
	 */
	protected BaseDAO(Class<T> clazz) {
		
		this.clazz = clazz;
		
	}

	/* (non-Javadoc)
	 * @see br.com.cadastro.persistencia.dao.DAO#save(T)
	 */
	 public void salvar(T entity) throws Exception {

		Session session = null;
		Transaction tx = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			session.saveOrUpdate(entity);
			tx.commit();
	 	} catch (Exception e) {
                    
          		if ( tx != null ) {
				tx.rollback();
			}
	 		throw e;
		} finally {
			tx = null;
			close(session);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see br.com.cadastro.persistencia.dao.DAO#remove(T)
	 */
	 public void excluir(T entity) throws Exception {
		
		Session session = null;
		Transaction tx = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			session.delete(entity);
			tx.commit();
		 } catch (Exception e) {
			if ( tx != null ) {
				tx.rollback();
			}
	 		throw e;
		} finally {
			tx = null;
			close(session);
		}

	}

	/* (non-Javadoc)
	 * @see br.com.cadastro.persistencia.dao.DAO#load(java.lang.Long)
	 */
	 @SuppressWarnings("unchecked")
	public T detalhar(Long id) throws Exception {
		
		Session session = null;
		try {
			session = getSession();
			//List<T> obj = (List<T>) session.createQuery(
				//	"from " + clazz.getSimpleName() + " where id = " + id).list();
			Object obj =  session.get(clazz, id.intValue());
			
			return (T) obj;
			
		} catch (Exception e) {
			throw e;
		} finally {
			close(session);
		}
		
		
		
	}
	
	/* (non-Javadoc)
	 * @see br.com.cadastro.persistencia.dao.DAO#loadAll()
	 */
	public List<T> listarTodos() throws Exception {
		
		Session session = null;
		try {
			session = getSession();
                        
                        session.beginTransaction();
                        
			List<T> obj = (List<T>) session.createQuery(
					"from " + clazz.getSimpleName()).list();
			
                        
			return obj;
			
		} catch (Exception e) {
			throw e;
		} finally {
			close(session);
		}
		
		
		
	}
         
         
	public List<T> listarAtivos() throws Exception {
		
		Session session = null;
		try {
			session = getSession();
                        
                        session.beginTransaction();
                        
			List<T> obj = (List<T>) session.createQuery(
					"from " + clazz.getSimpleName() +
                                        " where status =  '" + Constantes.STATUS_ATIVO+"'").list();
			
                        
			return obj;
			
		} catch (Exception e) {
			throw e;
		} finally {
			close(session);
		}
		
		
		
	}
	
        
	
	@SuppressWarnings("static-access")
	protected Session getSession() {
		return this.getSessionFactory().getCurrentSession();
	}

	protected void close(Session session) {
		if (session != null && session.isOpen()) {
			session.close();
		}
	}
	
}